﻿using Microsoft.Web.Administration;
using NLog;
using SharpCompress.Archives;
using SharpCompress.Archives.Zip;
using SharpCompress.Readers;
using SuperSocket.ClientEngine;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Text;
using System.Threading;
using WebSvrUpdaterCore.IISAdmin;
using WebSvrUpdaterCore.Models;

namespace WebSvrUpdaterCore
{
    class CommandHandle
    {
        private EasyClient<MsgPacket> _client;
        private readonly string _clientId;
        private readonly ILogger _logger;
        private readonly List<Site> _siteList;
        private readonly IPEndPoint _endPoint;
        public CommandHandle(EasyClient<MsgPacket> client,
            string clientId,
            ILogger logger,
            IPEndPoint endPoint)
        {
            _client = client;
            _clientId = clientId;
            _logger = logger;
            _siteList = IISHelper.GetAllSite();
            _endPoint = endPoint;
        }

        public void SendMsg(MsgPacket data)
        {
            if (data == null)
            {
                throw new ArgumentNullException("data参数不能为空");
            }
            if (_client != null)
            {
                if (!_client.IsConnected)
                {
                    var task = _client.ConnectAsync(_endPoint);
                    task.Wait();
                    if (!task.Result)
                    {
                        _logger.Error("SendMsg无法发送请求，原因：无法连接服务器");
                        return;
                    }
                }
                _client.Send(data.ToBytes());
            }
        }


        public void PingReply()
        {
            if (_client != null)
            {
                SendMsg(MsgPacket.CreateNewInstance(_clientId, MsgTypes.Ping, "0", ""));
                //Console.WriteLine("PingReply");
                _logger.Trace("PingReply");

            }
        }



        public void Ping()
        {
            if (_client != null)
            {
                SendMsg(MsgPacket.CreateNewInstance(_clientId, MsgTypes.Ping, "1", ""));
                //Console.WriteLine("发送ping");
                _logger.Trace("发送ping");
            }
        }

        public void Bye()
        {
            if (_client != null)
            {
                SendMsg(MsgPacket.CreateNewInstance(_clientId, MsgTypes.Bye, "", ""));
                //Console.WriteLine("PingReply");
                _logger.Trace("PingReply");

            }
        }

        public void GetTime()
        {
            SendMsg(MsgPacket.CreateNewInstance(_clientId, MsgTypes.Time, "", null));
        }

        /// <summary>
        /// 回应 查询站点版本信息
        /// </summary>
        /// <param name="e"></param>
        public void GetVersionReply(PackageEventArgs<MsgPacket> e)
        {
            _logger.Trace("GetVersionReply");
            if (_client != null)
            {
                var getVerReq = e.Package.ConvertBody<GetVersionRequest>();
                var requestId = e.Package.ReqesutId;
                if (getVerReq == null)
                {
                    _logger.Trace("GetVersionReply,getVerReq is null");
                    SendMsg(MsgPacket.CreateNewInstance(_clientId, MsgTypes.GetVersionReply, new GetVersionRespone(100201, "参数为空或格式错误,无法解析"), requestId));
                }
                else
                {
                    //获取站点名称
                    var site = IISHelper.GetSiteByName(getVerReq.SiteName);
                    if (site == null)
                    {
                        SendMsg(MsgPacket.CreateNewInstance(_clientId, MsgTypes.GetVersionReply, new GetVersionRespone(100210, "找不到站点"), requestId));
                    }
                    else
                    {
                        //取得站点物理路径
                        var rootApp = site.Applications["/"];
                        var rootVDir = rootApp.VirtualDirectories["/"];
                        var physicalPath = rootVDir.PhysicalPath;
                        if (Directory.Exists(physicalPath))
                        {
                            var fullPath = Path.Combine(physicalPath, getVerReq.FilePath);
                            if (File.Exists(fullPath))
                            {
                                AssemblyName assemblyName = AssemblyName.GetAssemblyName(fullPath);
                                var respone = new GetVersionRespone()
                                {
                                    SiteName = site.Name,
                                    AssemblyVersion = assemblyName.Version
                                };
                                SendMsg(MsgPacket.CreateNewInstance(_clientId, MsgTypes.GetVersionReply, respone, requestId));

                            }
                            else
                            {
                                SendMsg(MsgPacket.CreateNewInstance(_clientId, MsgTypes.GetVersionReply, new GetVersionRespone(100212, "FilePath错误"), requestId));
                            }

                        }
                        else
                        {
                            SendMsg(MsgPacket.CreateNewInstance(_clientId, MsgTypes.GetVersionReply, new GetVersionRespone(100211, "物理路径不存在"), requestId));
                        }
                    }

                }

            }
            else
            {
                _logger.Trace("GetVersionReply,_client is null or client Is not Connected");
            }
        }

        /// <summary>
        /// 检测更新
        /// </summary>
        /// <param name="e"></param>
        /// <param name="channel">使用那个更新通道,如果是生产环境最好选择3:稳定版</param>
        public void CheckUpdateReply(PackageEventArgs<MsgPacket> e, int channel = 3)
        {
            var requestId = "";
            _logger.Trace("CheckUpdateReply");
            if (_client != null)
            {
                var checkUpdateReq = e.Package.ConvertBody<CheckUpdateRequest>();
                if (checkUpdateReq == null)
                {
                    _logger.Trace("CheckUpdateReply,checkUpdateReq is null");
                    SendMsg(MsgPacket.CreateNewInstance(_clientId, MsgTypes.CheckUpdateReply, new CheckUpdateRespone(100201, "参数为空或格式错误,无法解析"), requestId));
                }
                else
                {
                     requestId = e.Package.ReqesutId;
                    if (string.IsNullOrEmpty(checkUpdateReq.AssemblyFilePath))
                    {
                        SendMsg(MsgPacket.CreateNewInstance(_clientId, MsgTypes.CheckUpdateReply, new CheckUpdateRespone(100201, "参数'AssemblyFilePath'不能为空"), requestId));
                        return;
                    }
                    //获取站点名称
                    var site = IISHelper.GetSiteByName(checkUpdateReq.SiteName);
                    if (site == null)
                    {
                        SendMsg(MsgPacket.CreateNewInstance(_clientId, MsgTypes.CheckUpdateReply, new CheckUpdateRespone(100210, "找不到站点"), requestId));
                    }
                    else
                    {
                        //取得站点物理路径
                        var rootApp = site.Applications["/"];
                        var rootVDir = rootApp.VirtualDirectories["/"];
                        var physicalPath = rootVDir.PhysicalPath;
                        if (Directory.Exists(physicalPath))
                        {
                            //读取本地文件版本号
                            var localFilePath = Path.Combine(physicalPath, checkUpdateReq.AssemblyFilePath);
                            if (!File.Exists(localFilePath))
                            {
                                SendMsg(MsgPacket.CreateNewInstance(_clientId, MsgTypes.CheckUpdateReply, new CheckUpdateRespone(100212, "FilePath路径文件找不到"), requestId));
                                return;
                            }
                            //判断本地版本号与远程版本号
                            AssemblyName assemblyName = AssemblyName.GetAssemblyName(localFilePath);
                            if (checkUpdateReq.LastVersion > assemblyName.Version && channel == checkUpdateReq.Channel)
                            {
                                string cmdResult = string.Empty;
                                string actionResult = string.Empty;
                                //执行更新操作
                                var tmpFolder = Path.Combine(Path.GetTempPath(), site.Name);
                                if (!Directory.Exists(tmpFolder))
                                {
                                    Directory.CreateDirectory(tmpFolder);
                                }

                                var saveFile = $"{site.Name }_{checkUpdateReq.LastVersion.ToString()}.zip";
                                saveFile = Path.Combine(tmpFolder, saveFile);
                                //下载更新包,更新包必须是一个zip压缩文件
                                if (Utils.NetworkUtil.DownloadFile(checkUpdateReq.UpdatePackUrl, saveFile) && File.Exists(saveFile))
                                {
                                    if (!string.IsNullOrEmpty(checkUpdateReq.UpdatePackMd5))
                                    {
                                        var md5Str = "";
                                        using (var file = File.Open(saveFile, FileMode.Open))
                                        {
                                            var hashBtyes = System.Security.Cryptography.MD5.Create().ComputeHash(file);
                                            md5Str = BitConverter.ToString(hashBtyes).Replace("-", "");
                                        }
                                        //检测文件md5值
                                        if (!checkUpdateReq.UpdatePackMd5.Equals(md5Str, StringComparison.OrdinalIgnoreCase))
                                        {
                                            SendMsg(MsgPacket.CreateNewInstance(_clientId, MsgTypes.CheckUpdateReply, new CheckUpdateRespone(100215, "文件md5校验失败"), requestId));
                                            Directory.Delete(tmpFolder, true);
                                            return;
                                        }
                                    }
                                    //直接解压文件到站点物理路径
                                    using (var reader = ZipArchive.Open(saveFile))
                                    {
                                        foreach (var entry in reader.Entries.Where(x => !x.IsDirectory))
                                        {
                                            entry.WriteToDirectory(physicalPath, new ExtractionOptions()
                                            {
                                                ExtractFullPath = true,
                                                Overwrite = true
                                            });
                                        }
                                    }
                                    //检测替换文件后文件版本号
                                    var newAssembly = AssemblyName.GetAssemblyName(localFilePath);
                                    if (checkUpdateReq.LastVersion < newAssembly.Version)
                                    {
                                        SendMsg(MsgPacket.CreateNewInstance(_clientId, MsgTypes.CheckUpdateReply, new CheckUpdateRespone(100214, "更新错误,替换更新文件失败或者更新文件版本错误"), requestId));
                                        Directory.Delete(tmpFolder, true);
                                        return;
                                    }
                                    //执行cmd命令
                                    if (!string.IsNullOrEmpty(checkUpdateReq.UpdateCommand))
                                    {
                                        try
                                        {
                                            var cmds = checkUpdateReq.UpdateCommand.Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries);
                                            cmdResult = Utils.Common.ExecuteCmd(cmds);
                                        }
                                        catch (Exception ex)
                                        {
                                            cmdResult = "更新执行cmd命令异常" + ex.Message;
                                            _logger.Error(ex, "更新执行cmd命令异常");
                                        }

                                    }
                                    //执行测试动作,一个http get 请求
                                    if (!string.IsNullOrEmpty(checkUpdateReq.TestAction))
                                    {
                                        try
                                        {
                                            actionResult = Utils.NetworkUtil.HttpGet(checkUpdateReq.TestAction);
                                        }
                                        catch (Exception ex)
                                        {
                                            actionResult = "更新执行TestAction测试异常,错误信息" + ex.Message;
                                            _logger.Error(ex, "更新执行TestAction测试异常");
                                        }
                                    }

                                }
                                else
                                {
                                    SendMsg(MsgPacket.CreateNewInstance(_clientId, MsgTypes.CheckUpdateReply, new CheckUpdateRespone(100214, "更新错误,下载更新包失败"), requestId));
                                    return;
                                }
                                //删除临时文件
                                Directory.Delete(tmpFolder, true);
                                SendMsg(MsgPacket.CreateNewInstance(_clientId, MsgTypes.CheckUpdateReply, new CheckUpdateRespone(0, "更新成功") { TestActionResult = actionResult, UpdateCommandResult = cmdResult, OldVersion = assemblyName.Version }, requestId));
                                return;
                            }
                            else
                            {
                                SendMsg(MsgPacket.CreateNewInstance(_clientId, MsgTypes.CheckUpdateReply, new CheckUpdateRespone(100213, "无需更新"), requestId));
                                return;
                            }
                        }
                        else
                        {
                            SendMsg(MsgPacket.CreateNewInstance(_clientId, MsgTypes.CheckUpdateReply, new CheckUpdateRespone(100211, "物理路径不存在"), requestId));
                        }
                    }

                }

            }
            else
            {
                _logger.Trace("CheckUpdateReply,_client is null or client Is not Connected");
            }
        }

        /// <summary>
        /// 回应 文件上传
        /// </summary>
        /// <param name="e"></param>
        public void UploadReply(PackageEventArgs<MsgPacket> e)
        {
            var requestId = "";

            try
            {
                var pak = e.Package;
                 requestId = pak?.ReqesutId;
                if (!pak.IsVaild)
                {
                    _logger.Warn("UploadReply,数据包校验错误");
                    SendMsg(MsgPacket.CreateNewInstance(_clientId, MsgTypes.UploadReply, new UploadRespone(100212, "数据包校验错误"), requestId));
                    return;
                }
                var request = pak.ConvertBody<UploadRequest>();
                var uploadUrl = request.UploadUrl;
                var uploadFileName = request.UploadFileName;
                var token = request.Token;
                if (string.IsNullOrEmpty(uploadUrl))
                {
                    SendMsg(MsgPacket.CreateNewInstance(_clientId, MsgTypes.UploadReply, new UploadRespone(100211, "UploadUrl不能为空"), requestId));
                    return;
                }
                if (request.FileFullPaths == null || request.FileFullPaths.Length < 1)
                {
                    SendMsg(MsgPacket.CreateNewInstance(_clientId, MsgTypes.UploadReply, new UploadRespone(100211, "FileFullPaths不能为空"), requestId));
                    return;
                }

                if (request.IsCompress)
                {
                    var savePath = Path.Combine(Path.GetTempPath(), uploadFileName + ".zip");
                    if (File.Exists(savePath))
                    {
                        File.Delete(savePath);
                    }
                    //把所有文件打包上传
                    _logger.Info($"打包上传文件开始");
                    using (var zipArc = ZipArchive.Create())
                    {
                        foreach (var f in request.FileFullPaths)
                        {
                            if (!string.IsNullOrEmpty(f) && File.Exists(f))
                            {
                                var name = Path.GetFileName(f);
                                zipArc.AddEntry(name, f);
                            }
                        }
                        zipArc.SaveTo(savePath, SharpCompress.Common.CompressionType.Deflate);
                    }
                    _logger.Info($"打包上传文件完成,文件路径:{savePath}");
                    if (File.Exists(savePath))
                    {
                        try
                        {
                            _logger.Info($"上传打包文件{savePath}开始");
                            var webClient = new WebClient();
                            webClient.Headers.Add(HttpRequestHeader.Authorization, "Bearer " + token);
                            var respone = webClient.UploadFile(uploadUrl, savePath);
                            var uploadResult = Encoding.UTF8.GetString(respone);
                            _logger.Info($"上传文件结束,结果:{uploadResult}");
                        }
                        catch (Exception ex)
                        {
                            _logger.Error(ex, $"上传打包文件{savePath}出错");
                            SendMsg(MsgPacket.CreateNewInstance(_clientId, MsgTypes.UploadReply, new UploadRespone(100216, "上传文件异常，错误信息：" + ex.Message), requestId));
                            return;
                        }
                        finally
                        {
                            File.Delete(savePath);
                        }
                    }
                }
                else
                {
                    //不打包,单独上传
                    int threadCount = 5;//上传线程数
                    int initCount = request.FileFullPaths.Length > threadCount ? threadCount : request.FileFullPaths.Length;
                    var semaphore = new Semaphore(initCount, threadCount);
                    foreach (var f in request.FileFullPaths)
                    {
                        if (!string.IsNullOrEmpty(f) && File.Exists(f))
                        {
                            var thread = new Thread(new ParameterizedThreadStart((p) =>
                            {
                                string uploadResult = "";
                                string filePath = "";
                                try
                                {
                                    semaphore.WaitOne();
                                    filePath = p as string;
                                    _logger.Info($"上传文件{filePath}开始");
                                    var webClient = new WebClient();
                                    webClient.Headers.Add(HttpRequestHeader.Authorization, "Bearer " + token);
                                    var respone = webClient.UploadFile(uploadUrl, filePath);
                                    uploadResult = Encoding.UTF8.GetString(respone);
                                }
                                catch (Exception ex)
                                {
                                    _logger.Error(ex, $"上传文件{filePath}出错");
                                }
                                finally
                                {
                                    _logger.Info($"上传文件{filePath}结束,Result:{uploadResult}");
                                    semaphore.Release();
                                }

                            }));
                            thread.IsBackground = true;
                            if (f.Length < 10)
                            {
                                thread.Name = f;
                            }
                            else
                            {
                                thread.Name = f.Substring(f.Length - 10);
                            }
                            thread.Start(f);
                        }

                    }
                }

                SendMsg(MsgPacket.CreateNewInstance(_clientId, MsgTypes.UploadReply, new UploadRespone(0, "文件上传任务已开始"), requestId));

            }
            catch (Exception ex)
            {
                _logger.Error(ex, "上传文件出错");
                SendMsg(MsgPacket.CreateNewInstance(_clientId, MsgTypes.UploadReply, new UploadRespone(100216, "上传文件异常，错误信息：" + ex.Message), requestId));
                return;
            }

        }

        /// <summary>
        /// 回应 文件下载
        /// </summary>
        /// <param name="e"></param>
        public void DownLoadReply(PackageEventArgs<MsgPacket> e)
        {
            var requestId = "";

            try
            {
                var pak = e.Package;
                 requestId = pak?.ReqesutId;
                if (!pak.IsVaild)
                {
                    _logger.Warn("DownLoadReply,数据包校验错误");
                    SendMsg(MsgPacket.CreateNewInstance(_clientId, MsgTypes.UploadReply, new UploadRespone(100212, "数据包校验错误"), requestId));
                    return;
                }
                var request = pak.ConvertBody<DownloadRequest>();
                if (request.Filelist == null || request.Filelist.Length < 1)
                {
                    SendMsg(MsgPacket.CreateNewInstance(_clientId, MsgTypes.DownLoadReply, new DownloadRespone(100211, "参数Filelist不能为空"), requestId));
                    return;
                }
                var savePath = Path.Combine(Path.GetTempPath(), DateTime.Now.ToString("Download_yyyyMMddsss"));

                if (!string.IsNullOrEmpty(request.SaveFullPath))
                {
                    savePath = request.SaveFullPath;
                }
                if (!Directory.Exists(savePath))
                {
                    Directory.CreateDirectory(savePath);
                }
                //多线程下载
                int threadCount = 5;//下载线程数
                int initCount = request.Filelist.Length > threadCount ? threadCount : request.Filelist.Length;
                var semaphore = new Semaphore(initCount, threadCount);
                foreach (var f in request.Filelist)
                {
                    var thread = new Thread(new ParameterizedThreadStart((p) =>
                    {
                        var downloadInfo = p as DownloadModel;
                        if (string.IsNullOrEmpty(downloadInfo.FileUrl))
                        {
                            _logger.Error("下载地址不能为空");
                        }
                        try
                        {
                            var filePath = Path.Combine(savePath, downloadInfo.FileName);
                            WebClient webClient = new WebClient();
                            webClient.DownloadFile(downloadInfo.FileUrl, filePath);
                            if (!string.IsNullOrEmpty(downloadInfo.FileMd5))
                            {
                                if (!Utils.Common.CheckFileMd5(filePath, downloadInfo.FileMd5))
                                {
                                    _logger.Error($"下载文件:{downloadInfo.FileUrl}的MD5值与服务器文件MD5值不匹配");
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            _logger.Error(ex, $"下载文件:{downloadInfo.FileUrl}出错");
                        }
                    }));
                }
                SendMsg(MsgPacket.CreateNewInstance(_clientId, MsgTypes.DownLoadReply, new DownloadRespone(0, "文件下载任务已开始"), requestId));

            }
            catch (Exception ex)
            {
                _logger.Error(ex, "下载文件出错");
                SendMsg(MsgPacket.CreateNewInstance(_clientId, MsgTypes.DownLoadReply, new DownloadRespone(100217, "下载文件异常，错误信息：" + ex.Message),requestId));
                return;
            }

        }

        public void WebSiteDeleteReply(PackageEventArgs<MsgPacket> e)
        {
            var requestId = "";
            try
            {
                var pak = e.Package;
                requestId = pak?.ReqesutId;
                if (!pak.IsVaild)
                {
                    _logger.Warn("WebSiteDeleteReply,数据包校验错误");
                    SendMsg(MsgPacket.CreateNewInstance(_clientId, MsgTypes.WebSiteDeleteReply, new WebSiteDeleteRespone(100212, "数据包校验错误"), requestId));
                    return;
                }
                var request = pak.ConvertBody<WebSiteDeleteRequest>();
                var siteName = request.SiteName;

                if (string.IsNullOrEmpty(siteName))
                {
                    SendMsg(MsgPacket.CreateNewInstance(_clientId, MsgTypes.WebSiteDeleteReply, new WebSiteDeleteRespone(100211, "参数siteName不能为空"), requestId));
                    return;
                }

                var site = IISHelper.GetSiteByName(siteName);
                if (site == null)
                {
                    SendMsg(MsgPacket.CreateNewInstance(_clientId, MsgTypes.WebSiteDeleteReply, new WebSiteDeleteRespone(100210, "删除失败,站点不存在"), requestId));
                    return;
                }
                //删除站点
                IISHelper.DeleteSite(site, request.IsDelAppPool);
                Thread.Sleep(50);
                if (IISHelper.GetSiteByName(siteName) == null)
                {
                    SendMsg(MsgPacket.CreateNewInstance(_clientId, MsgTypes.WebSiteDeleteReply, new WebSiteDeleteRespone(0, "删除成功"), requestId));
                    return;
                }
                else
                {
                    SendMsg(MsgPacket.CreateNewInstance(_clientId, MsgTypes.WebSiteDeleteReply, new WebSiteDeleteRespone(100210, "删除失败,可能权限不足"), requestId));
                    return;
                }
            }
            catch (Exception ex)
            {
                _logger.Error(ex, "删除站点出错");
                SendMsg(MsgPacket.CreateNewInstance(_clientId, MsgTypes.WebSiteDeleteReply, new WebSiteDeleteRespone(100221, "删除站点异常，错误信息：" + ex.Message),  requestId));
                return;
            }
        }

        public void WebSiteEditReply(PackageEventArgs<MsgPacket> e)
        {
            var requestId = "";
            try
            {
                var pak = e.Package;
                requestId = pak?.ReqesutId;
                if (!pak.IsVaild)
                {
                    _logger.Warn("WebSiteEditReply,数据包校验错误");
                    SendMsg(MsgPacket.CreateNewInstance(_clientId, MsgTypes.WebSiteEditReply, new WebSiteEditRespone(100212, "数据包校验错误"), requestId));
                    return;
                }
                var request = pak.ConvertBody<WebSiteEditRequest>();
                var siteModel = request.SiteModel;

                if (siteModel == null)
                {
                    SendMsg(MsgPacket.CreateNewInstance(_clientId, MsgTypes.WebSiteEditReply, new WebSiteEditRespone(100211, "参数SiteModel不能为空"), requestId));
                    return;
                }

                IISHelper.UpdateSite(siteModel);
                SendMsg(MsgPacket.CreateNewInstance(_clientId, MsgTypes.WebSiteEditReply, new WebSiteEditRespone(0, "修改站点成功"), requestId));

            }
            catch (Exception ex)
            {
                _logger.Error(ex, "修改站点出错");
                SendMsg(MsgPacket.CreateNewInstance(_clientId, MsgTypes.WebSiteEditReply, new WebSiteEditRespone(100220, "修改站点异常，错误信息：" + ex.Message), requestId));
                return;
            }
        }

        public void WebSiteAddReply(PackageEventArgs<MsgPacket> e)
        {
            var requestId = "";

            try
            {
                var pak = e.Package;
                requestId = pak.ReqesutId;
                if (!pak.IsVaild)
                {
                    _logger.Warn("WebSiteAddReply,数据包校验错误");
                    SendMsg(MsgPacket.CreateNewInstance(_clientId, MsgTypes.WebSiteAddReply, new WebSiteAddRespone(100212, "数据包校验错误"), requestId));
                    return;
                }
                var request = pak.ConvertBody<WebSiteAddRequest>();
                var siteModel = request.SiteModel;
                if (string.IsNullOrEmpty(siteModel.SiteName))
                {
                    SendMsg(MsgPacket.CreateNewInstance(_clientId, MsgTypes.WebSiteAddReply, new WebSiteAddRespone(100201, "站点名称不能为空"), requestId));
                    return;
                }

                var physicalPath = siteModel?.PhysicalPath;
                if (string.IsNullOrEmpty(physicalPath))
                {
                    physicalPath = @"D:\CMTSWEB";
                }
                physicalPath = Path.Combine(physicalPath, siteModel.SiteName);
                StringBuilder cmdResultBuff = new StringBuilder();
                string cmdResult = "";
                var lineChars = "".PadLeft(10, '#');
                string testActionResult = "";
                if (siteModel == null || string.IsNullOrEmpty(request.SiteInfoExtend.WebPacketUrl) || string.IsNullOrEmpty(physicalPath))
                {
                    SendMsg(MsgPacket.CreateNewInstance(_clientId, MsgTypes.WebSiteAddReply, new WebSiteAddRespone(100211, "参数WebPacketUrl或SiteModel或PhysicalPath不能为空"), requestId));
                    return;
                }
                //检测端口号是否可用
                foreach (var s in siteModel.Bindings)
                {
                    if (!IISHelper.CheckPort(s.IpAddress, s.Port))
                    {
                        SendMsg(MsgPacket.CreateNewInstance(_clientId, MsgTypes.WebSiteAddReply, new WebSiteAddRespone(100220, $"创建站点异常，错误信息：端口号【{s.Port}】被占用"), requestId));
                        return;
                    }
                }

                //下载web包文件
                var tmpPath = Path.Combine(Path.GetTempPath(), $"dl_web_{siteModel.SiteName}.zip");
                if (File.Exists(tmpPath))
                {
                    File.Delete(tmpPath);
                }
                if (Utils.NetworkUtil.DownloadFile(request.SiteInfoExtend.WebPacketUrl, tmpPath))
                {
                    //解压文件到PhysicalPath
                    if (File.Exists(tmpPath))
                    {
                        if (!Directory.Exists(physicalPath))
                        {
                            Directory.CreateDirectory(physicalPath);
                        }
                        using (var reader = ZipArchive.Open(tmpPath))
                        {
                            foreach (var entry in reader.Entries.Where(x => !x.IsDirectory))
                            {
                                entry.WriteToDirectory(physicalPath, new ExtractionOptions()
                                {
                                    ExtractFullPath = true,
                                    Overwrite = true
                                });
                            }
                        }
                        var assemblyFile ="";
                        var dirInfo = new DirectoryInfo(physicalPath);
                        var ambFileName = request.SiteInfoExtend.WebAssemblyFileName;
                        var findFiles = dirInfo.EnumerateFiles(ambFileName, SearchOption.AllDirectories);
                        if(findFiles != null && findFiles.Count() > 0)
                        {
                            foreach(var f in findFiles)
                            {
                                if(f.Name == ambFileName)
                                {
                                    assemblyFile = f.FullName;
                                    break;
                                }
                            }
                        }
                        if (!string.IsNullOrEmpty(assemblyFile) && File.Exists(assemblyFile))
                        {
                            //检测文件版本号
                            var assemblyName = AssemblyName.GetAssemblyName(assemblyFile);
                            var newAssemblyVersion = Version.Parse(request.SiteInfoExtend.WebVersion);
                            if (newAssemblyVersion != assemblyName.Version)
                            {
                                SendMsg(MsgPacket.CreateNewInstance(_clientId, MsgTypes.CheckUpdateReply, new CheckUpdateRespone(100214, "更新错误,替换更新文件失败或者更新文件版本错误"), requestId));
                                return;
                            }
                        }
                        siteModel.PhysicalPath = physicalPath;
                    }
                }
                else
                {
                    _logger.Error("下载web文件包失败");
                    SendMsg(MsgPacket.CreateNewInstance(_clientId, MsgTypes.WebSiteAddReply, new WebSiteAddRespone(100219, "创建站点错误，原因：下载web文件包失败"), requestId));
                    return;
                }


                //清理临时文件
                if (File.Exists(tmpPath))
                {
                    File.Delete(tmpPath);
                }
                //创建站点前执行的cmd命令
                if (!string.IsNullOrEmpty(request.SiteInfoExtend.BeforExecCommands))
                {
                    try
                    {
                        var cmds = request.SiteInfoExtend.BeforExecCommands.Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries);
                        var ret = Utils.Common.ExecuteCmd(cmds);
                        cmdResultBuff.AppendLine(lineChars);
                        cmdResultBuff.AppendLine("创建站点前执行命令结果");
                        cmdResultBuff.AppendLine(lineChars);
                        cmdResultBuff.AppendLine(ret);
                        _logger.Debug("创建站点前，执行命令：{0}，执行结果：{1}", cmds, ret);
                    }
                    catch (Exception ex)
                    {
                        cmdResult = "创建站点，执行cmd命令异常" + ex.Message;
                        _logger.Error(ex, $"创建站点，执行cmd命令异常,命令：{request.SiteInfoExtend.BeforExecCommands}");
                    }
                }
                var newSite = IISHelper.CreateSite(siteModel);

                

                if (newSite != null)
                {
                    //执行测试动作,一个http get 请求
                    if (!string.IsNullOrEmpty(request.SiteInfoExtend.TestAction))
                    {
                        try
                        {
                            var url = "";
                            if (request.SiteInfoExtend.TestAction.StartsWith("http"))
                            {
                                url = request.SiteInfoExtend.TestAction;
                            }
                            else
                            {
                                var firstBindInfo = request.SiteModel.Bindings[0];

                                var host = firstBindInfo.Host;
                                if (string.IsNullOrEmpty(host) && ("0.0.0.0" == firstBindInfo.IpAddress) || "*" == firstBindInfo.IpAddress)
                                {
                                    host = "localhost";
                                }
                                url = $"{firstBindInfo.Protocol}://{host}:{firstBindInfo.Port}/{request.SiteInfoExtend.TestAction.TrimStart('/')}";
                            }
                            testActionResult = Utils.NetworkUtil.HttpGet(url);
                            _logger.Debug($"测试动作URL：{url}，测试结果：{testActionResult}");

                        }
                        catch (Exception ex)
                        {
                            testActionResult = "执行TestAction测试异常,错误信息" + ex.Message;
                            _logger.Error(ex, $"执行TestAction测试异常,测试动作：{request.SiteInfoExtend.TestAction}");
                        }
                    }

                    //创建站点后执行的cmd命令
                    if (!string.IsNullOrEmpty(request.SiteInfoExtend.AfterExecCommands))
                    {
                        try
                        {
                            var cmds = request.SiteInfoExtend.AfterExecCommands.Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries);
                            var ret = Utils.Common.ExecuteCmd(cmds);
                            cmdResultBuff.AppendLine(lineChars);
                            cmdResultBuff.AppendLine("创建站点后执行命令结果");
                            cmdResultBuff.AppendLine(lineChars);
                            cmdResultBuff.AppendLine(ret);
                            _logger.Debug("创建站点后，执行命令：{0}，执行结果：{1}", cmds, ret);
                        }
                        catch (Exception ex)
                        {
                            cmdResult = "创建站点后，执行cmd命令异常" + ex.Message;
                            _logger.Error(ex, $"创建站点后，执行cmd命令异常,命令：{request.SiteInfoExtend.AfterExecCommands}");
                        }
                    }

                    cmdResult = cmdResultBuff.ToString();
                    SendMsg(MsgPacket.CreateNewInstance(_clientId, MsgTypes.WebSiteAddReply, new WebSiteAddRespone(0, "创建站点成功") { WebSiteId = newSite.Id, TestAtionResult = testActionResult, CmdResult = cmdResult,WebSitePath = physicalPath }, requestId));
                    _logger.Info($"创建站点成功，站点Id{newSite.Id}，站点名称：{newSite.Name}");
                }
                else
                {
                    SendMsg(MsgPacket.CreateNewInstance(_clientId, MsgTypes.WebSiteAddReply, new WebSiteAddRespone(100219, "创建站点失败"), requestId));
                }
            }
            catch (Exception ex)
            {
                _logger.Error(ex, "创建站点出错");
                SendMsg(MsgPacket.CreateNewInstance(_clientId, MsgTypes.WebSiteAddReply, new WebSiteAddRespone(100220, "创建站点异常，错误信息：" + ex.Message), requestId));
                return;
            }
        }

        public void WebSiteInfoReply(PackageEventArgs<MsgPacket> e)
        {
            var requestId = "";

            try
            {
                var pak = e.Package;
                requestId = pak.ReqesutId;
                if (!pak.IsVaild)
                {
                    _logger.Warn("WebSiteInfoReply,数据包校验错误");
                    SendMsg(MsgPacket.CreateNewInstance(_clientId, MsgTypes.WebSiteInfoReply, new WebSiteInfoRespone(100212, "数据包校验错误"), requestId));
                    return;
                }
                var request = pak.ConvertBody<WebSiteInfoRequest>();
                var siteName = request.SiteName;

                if (siteName == null)
                {
                    SendMsg(MsgPacket.CreateNewInstance(_clientId, MsgTypes.WebSiteInfoReply, new WebSiteInfoRespone(100211, "参数SiteName不能为空"), requestId));
                    return;
                }

                if (siteName == "#ALL#")
                {
                    var siteList = IISHelper.GetAllSite().Select(m =>
                    {
                        var limit = new SiteLimit
                        {
                            ConnectionTimeout = m.Limits.ConnectionTimeout.Milliseconds,
                            MaxBandwidth = m.Limits.MaxBandwidth,
                            MaxConnections = m.Limits.MaxConnections,
                            MaxUrlSegments = m.Limits.MaxUrlSegments
                        };
                        List<SiteBindingModel> siteBindingModels = new List<SiteBindingModel>();
                        bool bHttps = false;
                        foreach (var b in m.Bindings)
                        {
                            var binding = new SiteBindingModel
                            {
                                BindingInformation = b.BindingInformation,
                                CertificateHash = b.CertificateHash,
                                CertificateStoreName = b.CertificateStoreName,
                                IpAddress = b.EndPoint.Address.ToString(),
                                Port = b.EndPoint.Port,
                                Protocol = b.Protocol,
                                SslFlags = (int)b.SslFlags,
                                UseDsMapper = b.UseDsMapper
                            };
                            if (binding.SslFlags > 0)
                            {
                                bHttps = true;
                            }
                            siteBindingModels.Add(binding);
                        }

                        return new SiteModel
                        {
                            AppPoolName = m.ApplicationDefaults.ApplicationPoolName,
                            AutoStart = m.ServerAutoStart,
                            Status = (int)m.State,
                            Id = m.Id,
                            Limits = limit,
                            Bindings = siteBindingModels,
                            EnableHttps = bHttps,
                            SiteName = m.Name,
                            PhysicalPath = m.Applications["/"]?.VirtualDirectories["/"]?.PhysicalPath
                        };
                    }).ToArray();
                    SendMsg(MsgPacket.CreateNewInstance(_clientId, MsgTypes.WebSiteInfoReply, new WebSiteInfoRespone(0, "查询所有站点成功") { SiteInfos = siteList }, requestId));

                }
                else
                {
                    var site = IISHelper.GetAllSite().Where(w => w.Name == siteName).Select(m =>
                      {
                          var limit = new SiteLimit
                          {
                              ConnectionTimeout = m.Limits.ConnectionTimeout.Milliseconds,
                              MaxBandwidth = m.Limits.MaxBandwidth,
                              MaxConnections = m.Limits.MaxConnections,
                              MaxUrlSegments = m.Limits.MaxUrlSegments
                          };
                          List<SiteBindingModel> siteBindingModels = new List<SiteBindingModel>();
                          bool bHttps = false;
                          foreach (var b in m.Bindings)
                          {
                              var binding = new SiteBindingModel
                              {
                                  BindingInformation = b.BindingInformation,
                                  CertificateHash = b.CertificateHash,
                                  CertificateStoreName = b.CertificateStoreName,
                                  IpAddress = b.EndPoint.Address.ToString(),
                                  Port = b.EndPoint.Port,
                                  Protocol = b.Protocol,
                                  SslFlags = (int)b.SslFlags,
                                  UseDsMapper = b.UseDsMapper
                              };
                              if (binding.SslFlags > 0)
                              {
                                  bHttps = true;
                              }
                              siteBindingModels.Add(binding);
                          }

                          return new SiteModel
                          {
                              AppPoolName = m.ApplicationDefaults.ApplicationPoolName,
                              AutoStart = m.ServerAutoStart,
                              Status = (int)m.State,
                              Id = m.Id,
                              Limits = limit,
                              Bindings = siteBindingModels,
                              EnableHttps = bHttps,
                              SiteName = m.Name,
                              PhysicalPath = m.Applications["/"]?.VirtualDirectories["/"]?.PhysicalPath
                          };
                      }).FirstOrDefault();
                    SendMsg(MsgPacket.CreateNewInstance(_clientId, MsgTypes.WebSiteInfoReply, new WebSiteInfoRespone(0, $"查询{siteName}站点信息成功")
                    {
                        SiteInfos = new SiteModel[] { site }
                    }, requestId));

                }

            }
            catch (Exception ex)
            {
                _logger.Error(ex, "查询站点出错");
                SendMsg(MsgPacket.CreateNewInstance(_clientId, MsgTypes.WebSiteInfoReply, new WebSiteInfoRespone(100220, "查询站点异常，错误信息：" + ex.Message), requestId));
                return;
            }
        }

        public void AppPoolInfoReply(PackageEventArgs<MsgPacket> e)
        {
            var requestId = "";

            try
            {
                var pak = e.Package;
                 requestId = pak.ReqesutId;
                if (!pak.IsVaild)
                {
                    _logger.Warn("AppPoolInfoReply,数据包校验错误");
                    SendMsg(MsgPacket.CreateNewInstance(_clientId, MsgTypes.AppPoolInfoReply, new AppPoolInfoRespone(100212, "数据包校验错误"), requestId));
                    return;
                }
                var request = pak.ConvertBody<AppPoolInfoRequest>();
                var poolName = request.AppPoolName;
                if (string.IsNullOrEmpty(poolName))
                {
                    SendMsg(MsgPacket.CreateNewInstance(_clientId, MsgTypes.AppPoolInfoReply, new AppPoolInfoRespone(100201, "参数AppPoolName不能为空"), requestId));
                    return;
                }

                if (poolName == "#ALL_POOL#")
                {
                    var pools = IISHelper.GetAllAppPools().Select(m =>
                    {
                        var identity = new AppPoolIdentity
                        {
                            IdentityType = (int)m.ProcessModel.IdentityType,
                            LoadUserProfile = m.ProcessModel.LoadUserProfile,
                            Password = m.ProcessModel.Password,
                            UserName = m.ProcessModel.UserName
                        };
                        return new AppPoolModel
                        {
                            Name = m.Name,
                            Status = (int)m.State,
                            StartMode = (int)m.StartMode,
                            AutoStart = m.AutoStart,
                            Enable32BitAppOnWin64 = m.Enable32BitAppOnWin64,
                            ManagedPipelineMode = (int)m.ManagedPipelineMode,
                            ManagedRuntimeVersion = m.ManagedRuntimeVersion,
                            QueueLength = m.QueueLength,
                            Identity = identity
                        };
                    }).ToArray();

                    SendMsg(MsgPacket.CreateNewInstance(_clientId, MsgTypes.AppPoolInfoReply, new AppPoolInfoRespone(0, "获取所有程序池成功")
                    {
                        AppPoolModels = pools
                    }, requestId));

                }
                else
                {
                    var pool = IISHelper.GetAllAppPools().Where(w => w.Name == poolName).Select(m =>
                      {
                          var identity = new AppPoolIdentity
                          {
                              IdentityType = (int)m.ProcessModel.IdentityType,
                              LoadUserProfile = m.ProcessModel.LoadUserProfile,
                              Password = m.ProcessModel.Password,
                              UserName = m.ProcessModel.UserName
                          };
                          return new AppPoolModel
                          {
                              Name = m.Name,
                              Status = (int)m.State,
                              StartMode = (int)m.StartMode,
                              AutoStart = m.AutoStart,
                              Enable32BitAppOnWin64 = m.Enable32BitAppOnWin64,
                              ManagedPipelineMode = (int)m.ManagedPipelineMode,
                              ManagedRuntimeVersion = m.ManagedRuntimeVersion,
                              QueueLength = m.QueueLength,
                              Identity = identity
                          };
                      }).FirstOrDefault();

                    SendMsg(MsgPacket.CreateNewInstance(_clientId, MsgTypes.AppPoolInfoReply, new AppPoolInfoRespone(0, $"获取{poolName}程序池成功")
                    {
                        AppPoolModels = new AppPoolModel[] { pool }
                    }, requestId));
                }
            }
            catch (Exception ex)
            {
                _logger.Error(ex, "查询程序池出错");
                SendMsg(MsgPacket.CreateNewInstance(_clientId, MsgTypes.AppPoolInfoReply, new AppPoolInfoRespone(100222, "查询程序池异常，错误信息：" + ex.Message), requestId));
                return;
            }
        }

        public void AppPooAddReply(PackageEventArgs<MsgPacket> e)
        {
            var requestId = "";

            try
            {
                var pak = e.Package;
                requestId = pak.ReqesutId;
                if (!pak.IsVaild)
                {
                    _logger.Warn("AppPoolAddReply,数据包校验错误");
                    SendMsg(MsgPacket.CreateNewInstance(_clientId, MsgTypes.AppPoolAddReply, new AppPoolAddRespone(100212, "数据包校验错误"), requestId));
                    return;
                }
                var request = pak.ConvertBody<AppPoolAddRequest>();
                if (request.AppPoolModel == null)
                {
                    SendMsg(MsgPacket.CreateNewInstance(_clientId, MsgTypes.AppPoolAddReply, new AppPoolAddRespone(100201, "参数AppPoolModel不能为空"), requestId));
                    return;
                }

                var newPool = IISHelper.CreateAppPool(request.AppPoolModel);
                if (newPool != null)
                {
                    SendMsg(MsgPacket.CreateNewInstance(_clientId, MsgTypes.AppPoolAddReply, new AppPoolAddRespone(0, "创建程序池成功") { AppPoolName = newPool.Name }, requestId));
                    return;
                }
                else
                {
                    SendMsg(MsgPacket.CreateNewInstance(_clientId, MsgTypes.AppPoolAddReply, new AppPoolAddRespone(100223, "创建程序池失败"), requestId));
                    return;
                }
            }
            catch (Exception ex)
            {
                _logger.Error(ex, "创建程序池出错");
                SendMsg(MsgPacket.CreateNewInstance(_clientId, MsgTypes.AppPoolAddReply, new AppPoolAddRespone(100223, "创建程序池异常，错误信息：" + ex.Message), requestId));
                return;
            }
        }

        public void AppPoolEditReply(PackageEventArgs<MsgPacket> e)
        {
            var requestId = "";
            try
            {
                var pak = e.Package;
                requestId = pak.ReqesutId;
                if (!pak.IsVaild)
                {
                    _logger.Warn("AppPoolEditReply,数据包校验错误");
                    SendMsg(MsgPacket.CreateNewInstance(_clientId, MsgTypes.AppPoolEditReply, new AppPoolEditRespone(100212, "数据包校验错误"), requestId));
                    return;
                }
                var request = pak.ConvertBody<AppPoolEditRequest>();
                if (string.IsNullOrEmpty(request.AppPoolName) || request.AppPoolModel == null)
                {
                    SendMsg(MsgPacket.CreateNewInstance(_clientId, MsgTypes.AppPoolEditReply, new AppPoolEditRespone(100201, "参数AppPoolName或AppPoolModel不能为空"), requestId));
                    return;
                }

                var newPool = IISHelper.UpdateAppPool(request.AppPoolName, request.AppPoolModel);
                SendMsg(MsgPacket.CreateNewInstance(_clientId, MsgTypes.AppPoolEditReply, new AppPoolEditRespone(0, "修改程序池成功") { AppPoolName = newPool.Name }, requestId));
                _logger.Info($"修改程序池成功，程序池名称：{newPool.Name}");
            }
            catch (Exception ex)
            {
                _logger.Error(ex, "修改程序池出错");
                SendMsg(MsgPacket.CreateNewInstance(_clientId, MsgTypes.AppPoolEditReply, new AppPoolEditRespone(100224, "修改程序池异常，错误信息：" + ex.Message), requestId));
                return;
            }
        }

        public void AppPoolDeleteReply(PackageEventArgs<MsgPacket> e)
        {
            var requestId = "";
            try
            {
                var pak = e.Package;
                requestId = pak.ReqesutId;
                if (!pak.IsVaild)
                {
                    _logger.Warn("AppPoolDeleteReply,数据包校验错误");
                    SendMsg(MsgPacket.CreateNewInstance(_clientId, MsgTypes.AppPoolDeleteReply, new AppPoolDeleteRespone(100212, "数据包校验错误"), requestId));
                    return;
                }
                var request = pak.ConvertBody<AppPoolDeleteRequest>();
                if (string.IsNullOrEmpty(request.AppPoolName))
                {
                    SendMsg(MsgPacket.CreateNewInstance(_clientId, MsgTypes.AppPoolDeleteReply, new AppPoolDeleteRespone(100201, "参数AppPoolName不能为空"), requestId));
                    return;
                }
                IISHelper.DeleteAppPool(request.AppPoolName);
                Thread.Sleep(50);//等待50ms，否则可能导致抛异常
                var pool = IISHelper.GetAppPoolByName(request.AppPoolName);
                if (pool == null)
                {
                    _logger.Info($"删除程序池：{request.AppPoolName}成功");
                    SendMsg(MsgPacket.CreateNewInstance(_clientId, MsgTypes.AppPoolDeleteReply, new AppPoolDeleteRespone(0, $"删除程序池：{request.AppPoolName}成功"), requestId));
                    return;
                }
                else
                {
                    SendMsg(MsgPacket.CreateNewInstance(_clientId, MsgTypes.AppPoolDeleteReply, new AppPoolDeleteRespone(100225, $"删除程序池：{request.AppPoolName}失败"), requestId));
                    return;
                }
            }
            catch (Exception ex)
            {
                _logger.Error(ex, "删除程序池出错");
                SendMsg(MsgPacket.CreateNewInstance(_clientId, MsgTypes.AppPoolDeleteReply, new AppPoolDeleteRespone(100225, "删除程序池异常，错误信息：" + ex.Message), requestId));
                return;
            }
        }

        public void CheckSiteNameReply(PackageEventArgs<MsgPacket> e)
        {
            var requestId = "";
            try
            {
                var pak = e.Package;
                 requestId = pak.ReqesutId;
                if (!pak.IsVaild)
                {
                    _logger.Warn("CheckSiteNameReply,数据包校验错误");
                    SendMsg(MsgPacket.CreateNewInstance(_clientId, MsgTypes.CheckSiteNameReply, new CheckSiteNameRespone(100212, "数据包校验错误"), requestId));
                    return;
                }
                var request = pak.ConvertBody<CheckSiteNameRequest>();
                if (string.IsNullOrEmpty(request.SiteName))
                {
                    SendMsg(MsgPacket.CreateNewInstance(_clientId, MsgTypes.CheckSiteNameReply, new CheckSiteNameRespone(100201, "参数SiteName不能为空"), requestId));
                    return;
                }
                var site = IISHelper.GetSiteByName(request.SiteName);
                
                if (site == null)
                {
                    SendMsg(MsgPacket.CreateNewInstance(_clientId, MsgTypes.CheckSiteNameReply, new CheckSiteNameRespone(0, $"名称可用"), requestId));
                    return;
                }
                else
                {
                    SendMsg(MsgPacket.CreateNewInstance(_clientId, MsgTypes.CheckSiteNameReply, new CheckSiteNameRespone(-1, $"名称已占用"), requestId));
                    return;
                }
            }
            catch (Exception ex)
            {
                _logger.Error(ex, "检查站点名称出错");
                SendMsg(MsgPacket.CreateNewInstance(_clientId, MsgTypes.CheckSiteNameReply, new CheckSiteNameRespone(100225, "检查站点名称异常，错误信息：" + ex.Message), requestId));
                return;
            }
        }

        public void CheckPoolNameReply(PackageEventArgs<MsgPacket> e)
        {
            var requestId = "";
            try
            {
                var pak = e.Package;
                requestId = pak.ReqesutId;
                if (!pak.IsVaild)
                {
                    _logger.Warn("CheckPoolNameReply,数据包校验错误");
                    SendMsg(MsgPacket.CreateNewInstance(_clientId, MsgTypes.CheckPoolNameReply, new CheckPoolNameRespone(100212, "数据包校验错误"), requestId));
                    return;
                }
                var request = pak.ConvertBody<CheckPoolNameRequest>();
                if (string.IsNullOrEmpty(request.PoolName))
                {
                    SendMsg(MsgPacket.CreateNewInstance(_clientId, MsgTypes.CheckPoolNameReply, new CheckPoolNameRespone(100201, "参数PoolName不能为空"), requestId));
                    return;
                }
                var site = IISHelper.GetAppPoolByName(request.PoolName);

                if (site == null)
                {
                    SendMsg(MsgPacket.CreateNewInstance(_clientId, MsgTypes.CheckPoolNameReply, new CheckPoolNameRespone(0, $"名称可用"), requestId));
                    return;
                }
                else
                {
                    SendMsg(MsgPacket.CreateNewInstance(_clientId, MsgTypes.CheckPoolNameReply, new CheckPoolNameRespone(-1, $"名称已占用"), requestId));
                    return;
                }
            }
            catch (Exception ex)
            {
                _logger.Error(ex, "检查程序池名称出错");
                SendMsg(MsgPacket.CreateNewInstance(_clientId, MsgTypes.CheckPoolNameReply, new CheckPoolNameRespone(100225, "检查程序池名称异常，错误信息：" + ex.Message), requestId));
                return;
            }
        }
    }
}
